//
//  MBProgressHUD+Additions.m
//  YunXueBao
//
//  Created by 和系 on 2018/1/15.
//  Copyright © 2018年 hexi. All rights reserved.
//

#import "MBProgressHUD+Additions.h"
NSTimeInterval const kMinShowTime = 1.f;
NSTimeInterval const KHideAfterDelayTime = 2.f;
NSTimeInterval const kActivityMinDismissTime = 0.5f;

@implementation MBProgressHUD (Additions)
/**
 *  快速创建提示框 有菊花
 *
 *  @param message 提示信息
 *  @param view 显示视图
 *  @return hud
 */
+ (MBProgressHUD *)showActivityMessage:(NSString *)message
                                toView:(UIView *)view{
    if (!view)
        view = [UIApplication sharedApplication].delegate.window;    /**  快速显示提示信息  */
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    /**  < 显示动画效果 >  */
    hud.animationType = MBProgressHUDAnimationZoom;
    /**  < 文字内容 >  */
    hud.label.text = message;
    /**  < 影藏后移除视图 >  */
    hud.removeFromSuperViewOnHide = YES;
    /**  中间方框背景色  */
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.85f];
    /**  内容颜色  */
    hud.contentColor = [UIColor whiteColor];
    /**  < 最小显示时间 >  */
    hud.minShowTime = kActivityMinDismissTime;
    /**  < 蒙层颜色 >  */
    hud.backgroundView.color = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
    return hud;
}

/**
 显示提示文字
 
 @param message 提示信息
 @param view 显示视图
 */
+ (void)showMessage:(NSString *)message
             toView:(UIView *)view
         completion:(MBProgressHUDCompletionBlock)completion{
    if (!view)
        view = [UIApplication sharedApplication].delegate.window;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.85f];
    hud.contentColor = [UIColor whiteColor];
    hud.backgroundView.color = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
    [hud hideAnimated:YES afterDelay:KHideAfterDelayTime];
    hud.minShowTime = kMinShowTime;
    hud.completionBlock = completion;
}
/**
 *  自定义成功提示
 *
 *  @param success 提示文字
 *  @param view 显示视图
 */
+ (void)showSuccess:(NSString *)success
             toView:(UIView *)view
         completion:(MBProgressHUDCompletionBlock)completion{
    [self show:success icon:@"MBHUD_Success" view:view completion:completion];
}
/**
 *  自定义失败提示
 *
 *  @param error 提示文字
 *  @param view 显示视图
 */
+ (void)showError:(NSString *)error
           toView:(UIView *)view
       completion:(MBProgressHUDCompletionBlock)completion{
    [self show:error icon:@"MBHUD_Error" view:view completion:completion];
}
/**
 *  自定义提示信息
 *
 *  @param info 提示信息
 *  @param view 示视图
 */
+ (void)showInfo:(NSString *)info
          toView:(UIView *)view
      completion:(MBProgressHUDCompletionBlock)completion{
    [self show:info icon:@"MBHUD_Info" view:view completion:completion];
}

/**
 *  自定义警告提示
 *
 *  @param warning 提示信息
 *  @param view 示视图
 */
+ (void)showWarning:(NSString *)warning
             toView:(UIView *)view
         completion:(MBProgressHUDCompletionBlock)completion{
    [self show:warning icon:@"MBHUD_Warn" view:view completion:completion];
}

/**
 *  自定义提示框
 *
 *  @param text 提示文字
 *  @param icon 图片名称
 *  @param view 展示视图
 */
+ (void)show:(NSString *)text
        icon:(NSString *)icon
        view:(UIView *)view
  completion:(MBProgressHUDCompletionBlock)completion{
    [self hideHUD];
    if (view == nil)
        view = [UIApplication sharedApplication].delegate.window;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = text;
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:icon];
    hud.customView = imageView;
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.85f];
    hud.contentColor = [UIColor whiteColor];
    hud.backgroundView.color = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
    hud.minShowTime = kMinShowTime;
    [hud hideAnimated:YES afterDelay:KHideAfterDelayTime];
    hud.completionBlock = completion;
}
#pragma mark --------  提示有菊花  --------
#pragma mark
/**  < 只显示菊花 >  */
+ (MBProgressHUD *)showActivity{
    MBProgressHUD *hud = [self showActivityMessage:nil toView:nil];
    hud.square = YES;
    return hud;
}
/**  < 菊花带有文字 >  */
+ (MBProgressHUD *)showActivityMessage:(NSString *)message{
    MBProgressHUD *hud = [self showActivityMessage:message toView:nil];
    hud.square = YES;
    return hud;
}

+ (void)showMessage:(NSString *)message{
    [self showMessage:message toView:nil completion:nil];
}
+ (void)showSuccessMessage:(NSString *)success{
    [self showSuccess:success toView:nil completion:nil];
}
+ (void)showErrorMessage:(NSString *)error{
    [self showError:error toView:nil completion:nil];
    
}
+ (void)showWarningMessage:(NSString *)warning{
    [self showWarning:warning toView:nil completion:nil];
    
}
+ (void)showInfoMessage:(NSString *)info{
    [self showInfo:info toView:nil completion:nil];
    
}
#pragma mark --------  隐藏  --------
#pragma mark
+ (void)hideHUD{
    UIView *winView =(UIView*)[UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:winView animated:YES];
    [self hideHUDForView:[self getCurrentUIVC].view animated:YES];
}
+ (void)hideHUDForView:(UIView *)view{
    [self hideHUDForView:view animated:YES];
}
#pragma mark --------  Private Method  --------
#pragma mark
//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentWindowVC
{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows)
        {
            if (tempWindow.windowLevel == UIWindowLevelNormal)
            {
                window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
    {
        result = nextResponder;
    }
    else
    {
        result = window.rootViewController;
    }
    return  result;
}

+(UIViewController *)getCurrentUIVC
{
    UIViewController  *superVC = [[self class]  getCurrentWindowVC ];
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}
@end
